﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Windows.Forms;
using NTTDATA.BEENChina.ResultCertification.Checker;
using NTTDATA.BEENChina.ResultCertification.Config;
 
using NTTDATA.BEENChina.ResultCertification.model;
using NTTDATA.BEENChina.ResultCertification.Main.ServiceResultCertification;

namespace NTTDATA.BEENChina.ResultCertification.UI
{
    public partial class FormVerfityModify : Form
    {
        public FormVerfityModify()
        {
            InitializeComponent();

            InitVerfityList();
        }

        EnumModify Modify = EnumModify.Add;

        public FormVerfityModify(EnumCategoryType categoryType, EnumModify enumModify)
        {
            InitializeComponent();
            InitVerfityList(categoryType);
            Modify = enumModify;

        }

        public FormVerfityModify(EnumCategoryType categoryType, string verfityItemId, EnumModify enumModify)
        {
            InitializeComponent();
            InitVerfityList(categoryType);
            Modify = enumModify;

            this.listBoxVerfity.Enabled = false;
            LoadPropertys(verfityItemId);
        }

        //private DataStore.IVerfity verfityAccess = new VerfityAccess();

        public void InitVerfityList()
        {
            IEnumerable<Type> verfityClass = Checker.VerfityFactory.GetVerfitySubClass(); 
            initVerfityList(verfityClass); 

        }

        public void InitVerfityList( NTTDATA.BEENChina.ResultCertification.Config.EnumCategoryType categoryType)
        {
            IEnumerable<Type> verfityClass = Checker.VerfityFactory.GetVerfitySubClass( categoryType);
            initVerfityList(verfityClass);

        }

        private void initVerfityList(IEnumerable<Type> verfityClass)
        {

            List<VerfityAttribute> verfityTable = new List<VerfityAttribute>();

            foreach (var type in verfityClass)
            {
                System.Reflection.MemberInfo info = type;

                VerfityAttribute attribute 
                    = (VerfityAttribute)Attribute.GetCustomAttribute(info, typeof(VerfityAttribute));

                if (attribute != null)
                {

                    //类的全名，用于类的创建
                    attribute.ClassFullName = type.FullName;
                    verfityTable.Add(attribute);

                }
            }
       ;

            listBoxVerfity.DataSource = verfityTable.OrderBy(t => t.Id).ToList();
            listBoxVerfity.DisplayMember = "DisplayName";
            listBoxVerfity.ValueMember = "ClassFullName";

        }

        

        private void InitPropertys()
        {
            string verfityClassName = this.listBoxVerfity.SelectedValue.ToString();
            InitPropertys(verfityClassName);
        }

        /// <summary>
        /// 加载新属性
        /// </summary>
        /// <param name="verfityClassName"></param>
        private void InitPropertys(string verfityClassName)
        {          
            if (!String.IsNullOrEmpty(verfityClassName))
            {
                Verfity verfity =
                    VerfityFactory.CreateVerfityInstance(verfityClassName);
                /*"NTTDATA.BEENChina.ResultCertification.Checker.VerfityJapaneseCalendar");*/

                //默认检查fianalJob
                //verfity.VerfityRanks = EnumRank.Final;
                Verfity.SetPropertyReadOnly(verfity, "Id", false);
                propertyGrid1.SelectedObject = verfity;
            }
        }

        /// <summary>
        /// 加载已有的属性
        /// </summary>
        /// <param name="verfityItemId"></param>
        private void LoadPropertys(string verfityItemId)
        {
            using (ServiceResultCertificationClient client = new ServiceResultCertificationClient())
            {
                //VerfityItem verfityItem = verfityAccess.GetVerfityItem(verfityItemId);
                VerfityItem verfityItem = client.GetVerfityItem(verfityItemId);

              

                //Verfity verfity = new Util.Serializer().DeSerializeXml(typeof(Verfity), verfityItem.VerfityItemInstance) as Verfity;
                Verfity verfity =
                    new Util.Serializer().DeSerializeXml(typeof(Verfity), verfityItem.VerfityItemInstance) as Verfity;
                    //client.DeSerializeXml(typeof(Verfity), verfityItem.VerfityItemInstance);

                if (verfity != null)
                {
                    verfity = Verfity.SetPropertyReadOnly(verfity, "Id", true);
                    //用数据库字段里的name替换序列化字符里的name
                    verfity.Name = verfityItem.VerfityItemName;
                    
                    this.propertyGrid1.SelectedObject = verfity;
                }
            }            
        }

        private void buttonModify_Click(object sender, EventArgs e)
        {
            InitPropertys();
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            SavePropertys();
        }

        private void SavePropertys()
        {          
           Verfity verfity = ( Verfity)this.propertyGrid1.SelectedObject;
           if (verfity != null)
           {
               //insert
               if (  Modify == EnumModify.Add)
 
               {
                   //检查verfityID
                   bool bResult = checkInputProperties();

                   if (!bResult)
                       return;

                   VerfityItem verfityItem = GetModifyValue(verfity); 
                   
                   

                   using (ServiceResultCertificationClient client = new ServiceResultCertificationClient())
                   {
                       int effactRow = client.AddVerfityItem(verfityItem);
                       if (effactRow > 0)
                       {
                           MessageBox.Show(string.Format("检查类{0}创建成功。", verfityItem.VerfityItemId));
                       }
                   }
               }
               else if (Modify == EnumModify.Modify)
               {
                   //update
                  VerfityItem verfityItem = GetModifyValue(verfity);  

                   using (ServiceResultCertificationClient client = new ServiceResultCertificationClient())
                   { 
                       int effactRow = client.UpdateVerfityItem(verfityItem);
                       if (effactRow > 0)
                       {
                           MessageBox.Show(string.Format("检查类{0}修改成功。", verfityItem.VerfityItemId));
                       }
                   }

               }

           }
           
        }

        private VerfityItem GetModifyValue(Verfity verfity)
        {
            VerfityItem verfityItem = new VerfityItem();
            VerfityAttribute attribute = VerfityFactory.GetVerfityAttr(verfity.GetType());
            verfityItem.VerfityItemId = verfity.Id;
            verfityItem.VerfityItemName = verfity.Name;
            verfityItem.IsModelItem = attribute.Model;
            verfityItem.CategoryType = attribute.CategoryType;
            verfityItem.VerfityItemClassName = attribute.Id;
            verfityItem.Memo = verfity.Memo;
            verfityItem.VerfityItemInstance = Verfity.GetSerialize(verfity);

            return verfityItem;
 
        }

        private bool checkInputProperties()
        {
            bool verfityResult = false;

              Verfity verfity 
                 = ( Verfity)this.propertyGrid1.SelectedObject;
             if (verfity != null)
             {
                 
                 string verfityId = verfity.Id;

                 if (String.IsNullOrEmpty(verfityId))
                 {
                     MessageBox.Show( "请输入ID" );
                 }
                 else
                 {
                     //verfityResult = !verfityAccess.VerfityItemId(verfityId);
                     //if (!verfityResult)
                     //    MessageBox.Show(string.Format("ID:{0}已存在,请重新输入。", verfityId));

                     using (ServiceResultCertificationClient client = new ServiceResultCertificationClient())
                     {
                         verfityResult = client.VerfityItemId(verfityId);
                         if (!verfityResult)
                             MessageBox.Show(string.Format("ID:{0}已存在,请重新输入。", verfityId));
                     }
                 }                                 
             }
            return verfityResult;
        }

        private void buttonExit_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }
    }

    public enum EnumModify
    {
        Add,Modify,Delete
    }
}
